package com.example.raspberryPi_demo.controller;

import com.example.raspberryPi_demo.entity.Equipment;
import com.example.raspberryPi_demo.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path="/equipment")
public class EquipmentController {
    //注入 equipment sevice
    @Autowired
    private EquipmentService equipmentService;

    /**
     * 查询设备列表
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Equipment> getEquipments(){
        return equipmentService.findEquipmentList();
    }

    /**
     * 查询一个设备
     * @param macAddress
     * @return
     */
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public  Equipment getEquipmentByMacAddress(@PathVariable("macAddress") String macAddress){
        return equipmentService.findEquipmentByMacAddress(macAddress);
    }

    @RequestMapping(value = "/getByName",method = RequestMethod.GET)
    public  Equipment getEquipmentByName(@PathVariable("raspberrypiname") String raspberrypiname){
        return equipmentService.findEquipmentByName(raspberrypiname);
    }
    /**
     * 删除一个设备
     * @param macAddress
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public  String delEquipmentByRaspberrypiname(@PathVariable("macAddress") String macAddress){
        int flag=equipmentService.delete(macAddress);
        if(flag==1){
            return "delete success！";
        }else {
            return "delete fail！";
        }
    }

    /**
     * 修改设备信息
     * @param macAddress
     * @param raspberrypiname
     * @param updateinterval
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public  String updateEquipment(@PathVariable("macAddress")String macAddress , @RequestParam(value = "raspberrypiname",required = true)String raspberrypiname,
                              @RequestParam(value = "updateinterval" ,required = true)int updateinterval){

        Equipment equipment = new Equipment();
        equipment.setMacAddress(macAddress);
        equipment.setRaspberryPiName(raspberrypiname);
        equipment.setUpdateInterval(updateinterval);
                int t=equipmentService.update(macAddress,equipment);
        if(t==1){
            return "update success！";
        }else {
            return "update fail！";
        }
    }

    /**
     * 新增设备信息
     * @param macAddress
     * @param raspberrypiname
     * @param updateinterval
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public  String postEquipment( @RequestParam(value = "macAddress")String macAddress,@RequestParam(value = "raspberrypiname")String raspberrypiname,
                             @RequestParam(value = "updateinterval")int updateinterval) {
        Equipment equipment = new Equipment();
        equipment.setMacAddress(macAddress);
        equipment.setRaspberryPiName(raspberrypiname);
        equipment.setUpdateInterval(updateinterval);
        int t = equipmentService.save(equipment);
        if (t == 1) {
            return "新增成功";
        } else {
            return "新增失败";
        }
    }



    }
